import { mongo } from "../mogoConfig";


const run = async () => {
    const bookTB = mongo.db("my_test").collection("books")
    let cursor = bookTB.find({}).sort("length", -1)
    console.log("排序1------------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item, null, 2))
    }

    cursor = bookTB.find({}).sort({ "length": -1, "author": 1 })
    console.log("排序2------------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item, null, 2))
    }

    cursor = bookTB.find({}).sort({ "length": -1, "author": 1 }).skip(2)
    console.log("排序3跳过前2个------------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item, null, 2))
    }

    cursor = bookTB.find({}, {
        sort: {
            "length": -1, "author": 1
        },
        skip: 2
    })
    console.log("排序4跳过前2个------------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item, null, 2))
    }

    cursor = bookTB.find({}).sort({ "length": -1, "author": 1 }).skip(2).limit(1)
    console.log("排序5分页------------------------------")
    for await (const item of cursor) {
        console.log(JSON.stringify(item, null, 2))
    }


}

run()
    .catch(error => {
        console.log(error)
    })
    .finally(() => {
        console.log('执行结束')
        mongo.close()
    })